package com.lixw.saa.controller;

import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: Embed2VectorController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/4 10:47
 **/
@RestController
public class Embed2VectorController {
    @Resource
    private EmbeddingModel embeddingModel;

    @Resource
    private VectorStore vectorStore;

    @GetMapping("/text2embed")
    public EmbeddingResponse embed2Vector(@RequestParam("msg") String msg) {
        EmbeddingResponse embeddingResponse = embeddingModel.call(new EmbeddingRequest(List.of(msg), DashScopeEmbeddingOptions.builder().withModel("text-embedding-v3").build()));
        System.out.println(embeddingResponse.getResult().getOutput());
        return embeddingResponse;
    }

    @GetMapping("/embed2vector/add")
    public void add(){
        List<Document> documents = List.of(
                new Document("i study LLM"),
                new Document("i love java")
        );
        vectorStore.add(documents);
    }

    @GetMapping("/embed2vector/get")
    public List getAll(@RequestParam(name = "msg") String msg){
        SearchRequest searchRequest = SearchRequest.builder()
                .query(msg)
                .topK(2)
                .build();
        List<Document> list = vectorStore.similaritySearch(searchRequest);
        System.out.println(list);
        return list;
    }

}